﻿Imports System.Data.SqlClient

Partial Class Tickets_Voir
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Change form mode to edit
    ''' </summary>
    Protected Sub ModifierTicket(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.FormViewTicket.ChangeMode(FormViewMode.Edit)
    End Sub

    ''' <summary>
    ''' Add a new comment
    ''' </summary>
    Protected Sub NouveauCommentaire(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim commentaire As String = CType(Me.FormViewTicket.FindControl("FCKeditorNouveauCommentaire"), FredCK.FCKeditorV2.FCKeditor).Value

        If commentaire <> "" Then
            Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("HelpDeskCS").ToString)
            conn.Open()

            Dim ticket_id As Integer = Request.QueryString("ticket_id")

            Dim cmd As New SqlCommand("INSERT INTO Commentaire (ticket_id, commentaire_auteur, commentaire_contenu)" & _
            " VALUES (@ticket_id, @commentaire_auteur, @commentaire_contenu)", conn)
            With cmd.Parameters
                .Add(New SqlParameter("ticket_id", ticket_id))
                .Add(New SqlParameter("commentaire_auteur", HttpContext.Current.User.Identity.Name))
                .Add(New SqlParameter("commentaire_contenu", commentaire))
            End With
            cmd.ExecuteNonQuery()

            conn.Close()

            CType(Me.FormViewTicket.FindControl("FCKeditorNouveauCommentaire"), FredCK.FCKeditorV2.FCKeditor).Value = ""

        End If

    End Sub

    ''' <summary>
    ''' Close the ticket
    ''' </summary>
    Protected Sub FermerTicket(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ticket_id As Integer = CInt(Request.QueryString("ticket_id"))

        Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("HelpDeskCS").ToString)
        conn.Open()
        Dim cmd As New SqlCommand("UPDATE Ticket SET ticket_resolu = 1, ticket_dateresolution = GETDATE() WHERE ticket_id = @ticket_id", conn)
        cmd.Parameters.Add(New SqlParameter("ticket_id", ticket_id))
        cmd.ExecuteNonQuery()
        conn.Close()

        Response.Redirect("Liste.aspx")
    End Sub

    ''' <summary>
    ''' Open the ticket
    ''' </summary>
    Protected Sub OuvrirTicket(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ticket_id As Integer = CInt(Request.QueryString("ticket_id"))

        Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("HelpDeskCS").ToString)
        conn.Open()
        Dim cmd As New SqlCommand("UPDATE Ticket SET ticket_resolu = 0, ticket_dateresolution = GETDATE() WHERE ticket_id = @ticket_id", conn)
        cmd.Parameters.Add(New SqlParameter("ticket_id", ticket_id))
        cmd.ExecuteNonQuery()
        conn.Close()

        Response.Redirect("TicketsForm.aspx?ticket_id=" & ticket_id)
    End Sub

End Class
